//
//  MTSplashView.m
//  MobTimes
//
//  Created by 江山 王 on 5/20/12.
//  Copyright (c) 2012 Finalist. All rights reserved.
//

#define SPLASH_LOADING_SPOT_STARTAT_X                       (110/2.0)
#define SPLASH_LOADING_SPOT_STARTAT_Y                       (559.5/2.0)
#define SPLASH_LOADING_SPOT_WIDTH                           (10/2.0)
#define SPLASH_LOADING_SPOT_HEIGHT                          (10/2.0)

#import "MTSplashView.h"

@implementation MTSplashView

- (id)init {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        _splashImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height)];
        _splashImageView.image = [UIImage imageNamed:@"splash.png"];
        [self addSubview:_splashImageView];
        
        _loadingSpot = [[UIImageView alloc] initWithFrame:CGRectMake(SPLASH_LOADING_SPOT_STARTAT_X, 
                                                                     SPLASH_LOADING_SPOT_STARTAT_Y, 
                                                                     SPLASH_LOADING_SPOT_WIDTH, 
                                                                     SPLASH_LOADING_SPOT_HEIGHT)];
        _loadingSpot.image = [UIImage imageNamed:@"splash_loading_spot.png"];
        [self addSubview:_loadingSpot];
        _loadingSpot.hidden = YES;
        
        _loadingProgress = [[UIView alloc] initWithFrame:CGRectMake(SPLASH_LOADING_SPOT_STARTAT_X,
                                                                         SPLASH_LOADING_SPOT_STARTAT_Y+2.5,
                                                                         1, 0.5)];
        [_loadingProgress setBackgroundColor:[UIColor colorWithRed:233/255.0f green:95/255.0f blue:34/255.0f alpha:1]];
        [self addSubview:_loadingProgress];
        _loadingProgress.hidden = YES;
    }
    return self;
}

- (void)startSplashLoading {
    _loadingSpot.hidden = NO;
    _loadingProgress.hidden = NO;
    
    [UIView beginAnimations:nil context:nil];
    CGRect _loadingSpotFrame = _loadingSpot.frame;
    _loadingSpotFrame.origin.x += 20;
    _loadingSpot.frame = _loadingSpotFrame;
    CGRect _loadingProgressFrame = _loadingProgress.frame;
    _loadingProgressFrame.size.width += 20;
    _loadingProgress.frame = _loadingProgressFrame;
    [UIView setAnimationDuration:0.01];
    if (_loadingSpotFrame.origin.x >= 263) {
        [UIView setAnimationDelegate:[UIApplication sharedApplication].delegate];
        [UIView setAnimationDidStopSelector:@selector(showMainViews)];
        [self release];
        [self removeFromSuperview];
    } else {
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(startSplashLoading)];
    }
    [UIView commitAnimations];
}

- (void)dealloc {
    [_splashImageView release];
    _splashImageView = nil;
    [_loadingSpot release];
    _loadingSpot = nil;
    [super dealloc];
}

@end
